/*
 * MafiaCardDeck.java $version 2011. 5. 19.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.luffy.mafia.carddeck;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.luffy.mafia.card.CharacterCard;
import com.luffy.mafia.character.MafiaCharacterType;

/**
 * 게임의 각 캐릭터를 선택하기 위한 카드덱.
 * <pre>
 * 파일명          : MafiaCardDeck.java
 * 작성일          : 2011. 5. 19.
 * 버젼            : 1.0
 * 수정내용
 * -----------------------------------------------------------------
 * 수정일           버젼      수정자       수정내용
 * -----------------------------------------------------------------
 * </pre>
 *
 * @author LuffeEve
 */
public class MafiaCardDeck {
	private List<CharacterCard> cardList = new ArrayList<CharacterCard>();
	@Autowired
	private CardFactory cardFactory;
	
	public void init(int playerCount) {
		createCardList(playerCount);
	}

	/**
	 * 전체 카드리스트를 생성한다.
	 */
	private void createCardList(int playerCount) {
		cardList.clear();
		
		int mafiaCardCount = (playerCount - 2) / 2;
		int doctorCardCount = 1;
		int policeCardCount = 1;
		
		for (int i = 0; i < playerCount; i++) {
			if (mafiaCardCount > 0) {
				cardList.add(cardFactory.getCard(MafiaCharacterType.MAFIA));
				mafiaCardCount--;
			} else if (doctorCardCount > 0) {
				cardList.add(cardFactory.getCard(MafiaCharacterType.DOCTOR));
				doctorCardCount--;
			} else if (policeCardCount > 0) {
				cardList.add(cardFactory.getCard(MafiaCharacterType.POLICE));
				policeCardCount--;
			} else{
				cardList.add(cardFactory.getCard(MafiaCharacterType.CITIZEN));				
			}
		}
		shuffle();
	}

	/**
	 * 카드를 섞는다.
	 */
	private void shuffle() {
		for (int i = 0; i < cardList.size(); i++) {
			Random random = new Random();
			CharacterCard card = cardList.remove(random.nextInt(cardList.size()));
			cardList.add(card);			
		}
	}

	/**
	 * 덱에서 한장의 카드를 고른다.
	 */
	public CharacterCard popOneCard() {
		return cardList.remove(0);
	}
}
